package com.csc.service;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csc.bean.BlogLogonBean;
import com.csc.dao.SystemTimeUsedDAO;
import com.csc.domain.SystemTimeUsed;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import constants.Constant;
/**
 * 
 * @author Nguyen Vu Hung
 *
 */
@Service
public class SystemTimeUsedServiceImpl implements SystemTimeUsedService {
	@Autowired
	SystemTimeUsedDAO sysTimeDao;

	static Logger logger = Logger.getLogger(SystemAccountService.class);

	public boolean addSystemTimeUsed(SystemTimeUsed sysTimeUsed) {
		return sysTimeDao.addSystemTimeUsed(sysTimeUsed);
	}

	public List<SystemTimeUsed> getAllListSystemTimeUsed() {
		return sysTimeDao.getAllListSystemTimeUsed();
	}

	// Search in blog logon by name
	public List<SystemTimeUsed> searchByName(String name) {
		List<SystemTimeUsed> listAll = sysTimeDao.getAllListSystemTimeUsed();
		List<SystemTimeUsed> listSearch = new ArrayList<SystemTimeUsed>();
		for (SystemTimeUsed sysUsed : listAll) {
			String fullName = sysUsed.getSysAccount().getAccountFirstName()
					+ " " + sysUsed.getSysAccount().getAccountMidName()
					+ " " + sysUsed.getSysAccount().getAccountLastName();
			if (fullName.contains(name))
				listSearch.add(sysUsed);
		}
		return listSearch;
	}
	
	// Search advanced blog logon
	public List<SystemTimeUsed> searchAdvancedBlogon(String name, String role, String dateTo, String dateFrom) {
		if (name.equals("null"))
			name = "";
		if (role.equals("null"))
			role = "";
		if (dateFrom.equals("null"))
			dateFrom = "";
		if (dateTo.equals("null"))
			dateTo = "";
		
		dateTo = sysTimeDao.getToDate(dateTo);
		dateFrom = sysTimeDao.getFromDate(dateFrom);
		return sysTimeDao.searchAdvance(name, role, dateTo, dateFrom);
	}
	
	public List<BlogLogonBean> parseToBlogLogon(List<SystemTimeUsed> listAll) {
		List<BlogLogonBean> listBlogLogon = new ArrayList<BlogLogonBean>();
		String userName = "";
		String fullName = "";
		String role = "";
		String task = "";
		String time = "";
		for (SystemTimeUsed sysTime : listAll) {
			userName = sysTime.getSysAccount().getAccountLoginId();
			fullName = sysTime.getSysAccount().getAccountFirstName() + " "
					+ sysTime.getSysAccount().getAccountMidName() + " "
					+ sysTime.getSysAccount().getAccountLastName();
			role = sysTime.getSysAccount().getSystemAccountType().getTypeName();
			task = sysTime.getActionType();
			time = sysTime.getTimeAction();

			listBlogLogon.add(new BlogLogonBean(userName, fullName, role, task,
					time));
		}
		return listBlogLogon;
	}
	

	// Export file pdf in Blog Logon
	public PdfPTable createTableBlogLogon(List<BlogLogonBean> listTrans,
			Image image) throws ParseException,
			DocumentException, IOException {
		// A table with three columns
		PdfPTable table = new PdfPTable(6);
		table.setWidthPercentage(100);
		table.setWidths(new float[] { 0.5f, 2f, 3f, 2f, 3f, 2f });
		// the cell object
		PdfPCell cell;
		// we add a cell with colspan 3
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		String timeAction = dateFormat.format(date);

		Font font1 = new Font(Font.FontFamily.HELVETICA, 25, Font.BOLDITALIC);
		Font font2 = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);
		Font font3 = new Font(Font.FontFamily.HELVETICA, 11, Font.NORMAL);
		Font font4 = new Font(Font.FontFamily.HELVETICA, 11, Font.NORMAL,
				BaseColor.GREEN);
		Font font5 = new Font(Font.FontFamily.HELVETICA, 11, Font.NORMAL,
				BaseColor.RED);

		// Add row CSC BANKING
		BaseColor colerBanner = new BaseColor(100, 100, 100);
		cell = new PdfPCell(new Phrase("CSC BANKING", font1));
		cell.setImage(image);
		cell.setBorderWidth(0);
		cell.setPaddingBottom(10);
		cell.setPaddingTop(5);
		cell.setBackgroundColor(colerBanner);
		cell.setColspan(6);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("  ", font1));
		cell.setBorderWidth(0);
		cell.setPadding(15);
		cell.setColspan(6);
		table.addCell(cell);

		// Add row Report and datime
		cell = new PdfPCell(new Phrase("Report Login - Logout.", font2));
		cell.setBorderWidth(0);
		cell.setPaddingBottom(30);
		cell.setColspan(5);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase(timeAction, font3));
		cell.setBorderWidth(0);
		cell.setPaddingBottom(30);
		cell.setColspan(1);
		table.addCell(cell);

		BaseColor coler = new BaseColor(100, 100, 100);
		cell = new PdfPCell(new Phrase("No"));
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setBorderWidthLeft(0);
		cell.setBorderWidthRight(0);
		cell.setBackgroundColor(coler);

		cell.setColspan(1);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("Username"));
		cell.setBorderWidthLeft(0);
		cell.setBorderWidthRight(0);
		cell.setBackgroundColor(coler);
		cell.setColspan(1);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("Full Name"));
		cell.setBorderWidthLeft(0);
		cell.setBorderWidthRight(0);
		cell.setBackgroundColor(coler);
		cell.setColspan(1);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("Role"));
		cell.setBorderWidthLeft(0);
		cell.setBorderWidthRight(0);
		cell.setBackgroundColor(coler);
		cell.setColspan(1);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("Task"));
		cell.setBorderWidthLeft(0);
		cell.setBorderWidthRight(0);
		cell.setBackgroundColor(coler);
		cell.setColspan(1);
		table.addCell(cell);

		cell = new PdfPCell(new Phrase("Time"));
		cell.setBorderWidthLeft(0);
		cell.setBorderWidthRight(0);
		cell.setBackgroundColor(coler);
		cell.setColspan(1);
		table.addCell(cell);

		for (int i = 0; i < listTrans.size(); i++) {
			table.addCell(String.valueOf(i + 1));
			table.addCell(listTrans.get(i).getUserName());
			table.addCell(listTrans.get(i).getFullName());
			table.addCell(listTrans.get(i).getRole());

			if (listTrans.get(i).getTask().equals(Constant.ACTION_LOGIN)) {
				cell = new PdfPCell(new Phrase(listTrans.get(i).getTask(),
						font4));
				table.addCell(cell);
			} else {
				cell = new PdfPCell(new Phrase(listTrans.get(i).getTask(),
						font5));
				table.addCell(cell);
			}
			table.addCell(listTrans.get(i).getTime());
		}
		return table;
	}
}
